from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import BrandsModels
from .serializers import BrandsSer
from django.core.paginator import Paginator
import os
from django.conf import settings
# Create your views here.

# 获取全部数据

class BrandsView(APIView):
    def get(self,request):
        # 接受前端数据
        page = request.query_params.get('page')
        pagesize = request.query_params.get('pagesize')
        print('接受的前端数据',page,pagesize)

        brands = BrandsModels.objects.all()
        count = brands.count()

        deng = Paginator(brands,pagesize)

        dada = deng.page(page)

        ser = BrandsSer(dada,many=True)

        return Response({
            'code':200,
            'msg':'获取数据成功',
            'count':count,
            'results':ser.data,
        })

    def post(self,request):
        # 接受前端数据
        name = request.data.get('name')
        first_name = request.data.get('first_name')
        logo = request.data.get('logo')

        if not all([name,first_name,logo]):
            return Response({
                'code':500,
                'msg':'数据不可为空'
            })

        try:
            BrandsModels.objects.create(
                name=name,
                first_name=first_name,
                logo=logo,
            )
            return Response({
                'code':201,
                'msg':'添加成功'
            })
        except Exception as e:
            print('添加失败',e)
            return Response({
                'code':500,
                'msg':'添加失败',
            })

class Upload(APIView):
    def post(self,request):
        # 接受前端文件
        files_name = request.FILES.get('file')
        print('大师傅三',files_name)

        file_dir = os.path.join(settings.STATICFILES_DIRS[0], "image/")

        filename = file_dir + files_name.name
        # 写入字节流
        with open(filename, "wb") as f:
            f.write(files_name.file.read())

        return Response({
            'code':200,
            "file_path": "/static/image/",
            'file_name':files_name.name
        })


# 修改和删除
class ShowView(APIView):
    def get(self,request, id):

        brand = BrandsModels.objects.filter(id=id)
        print('dasdsa',brand)

        res = BrandsSer(brand,many=True)

        return Response({
            'code':200,
            'msg':'获取成功',
            'data':res.data
        })

    def put(self, request, id):
        # 接受前端数据
        name = request.data.get('name')
        first_name = request.data.get('first_name')
        logo = request.data.get('logo')
        print('接受到的数据',name,first_name,logo)

        # 判断
        if not all([name,first_name,logo]):
            return Response({
                'code':400,
                'msg':'数据不可为空'
            })

        try:
            brand = BrandsModels.objects.get(id=id)
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':400,
                'msg':'添加信息失败，没有此id'
            })
        try:
            brand.name = name
            brand.first_name = first_name
            brand.logo = logo
            brand.save()
        except Exception as e:
            print('修改数据失败',e)
            return Response({
                'code':400,
                'msg':'修改数据失败'
            })

        return Response({
            'code':200,
            'msg':'修改数据成功',
        })

    def delete(self, request,id):
        try:
            BrandsModels.objects.get(id=id).delete()
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code':204,
            'msg':'删除成功'
        })










